package com.x.erp.manage;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.dao.MarketDao;
import com.x.erp.dao.UserDao;
import com.x.erp.model.Market;
import com.x.erp.model.User;
import com.x.erp.util.Constant;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.manage.BaseManage;
import com.x.security.model.Account;
import com.x.security.rpc.SecurityRpcService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserManage extends BaseManage {

	@Autowired
	private UserDao userDao;

	@Autowired
	private MarketDao marketDao;

	@Reference
	private SecurityRpcService securityRpcService;


	public User insertUser(User user) throws Exception {
		Date date = this.getDate(0);
		user.setUserCreateTime(date);
		user.setUserUpdateTime(date);
		// 创建用户编号
		createUserNo(user);
		user = this.insert(user);
		String userId = user.getUserId();

		Map<String, String> accountPropertyMap = new HashMap<String, String>();
		accountPropertyMap.put("userId", userId);
		accountPropertyMap.put("marketId", user.getMarketId());
		if (this.isNotNull(user.getShopId())) {
			accountPropertyMap.put("shopId", user.getShopId());
		}
		Account account = new Account();
		account.setAccountName(user.getUserMobile());
		account.setAccountPassword(user.getUserMobile().substring(5));
		account.setAccountRemark(user.getUserRemark() + user.getUserName());
		account.setAccountState(0);
		account.setAccountCreateTime(date);
		account.setAccountModifyTime(date);
		account.setAccountPropertyMap(accountPropertyMap);
		account = securityRpcService.insertAccount(account, user.getUserType() + Constant.blank);
		String accountId = account.getAccountId();

		user = new User();
		user.setUserId(userId);
		user.setAccountId(accountId);
		this.update(user);
		return user;
	}

	// 生成用户编号
	private void createUserNo(User user) throws Exception {
		// 获取市场编号
		Market marketTemp = new Market();
		marketTemp.setMarketId(user.getMarketId());
		List<Market> list =  marketDao.selectMarketList(marketTemp);
		if(list == null || list.size() == 0){
			throw new BusinessException("创建用户时，市场Id不正确！");
		}
		marketTemp = list.get(0);
		String marketCode = marketTemp.getMarketCode();

		// 获取当前市场用户的最大用户编号
		Map<String, String> temp = userDao.selectMaxUserNo(user);
		String maxUserNo = temp.get("userNo");
		// 将查询的用户编号获取后4位，数字加1
		Integer userNum = Integer.valueOf(maxUserNo.substring(maxUserNo.length() - 4)) + 1;
		// 将用户编号格式化成4位字符串
		String userCode = String.format("%04d", userNum);
		// 拼接用户编号
		String userNo = marketCode + userCode;
		// 为用户赋值员工编号
		user.setUserNo(userNo);
	}

	public Integer updateUser(User user) throws Exception {
		Date date = this.getDate(0);
		if (user.getAccountId() != null) {
			Account account = new Account();
			account.setAccountId(user.getAccountId());
			if (StringUtils.isNotEmpty(user.getUserMobile())) {
				account.setAccountName(user.getUserMobile());
			}
			if (StringUtils.isNotEmpty(user.getMarketId())) {
				Map<String, String> accountPropertyMap = new HashMap<String, String>();
				accountPropertyMap.put("userId", user.getUserId());
				accountPropertyMap.put("marketId", user.getMarketId());
				if (StringUtils.isNotEmpty(user.getShopId())) {
					accountPropertyMap.put("shopId", user.getShopId());
				}
				account.setAccountPropertyMap(accountPropertyMap);
			}
			if (StringUtils.isNotEmpty(user.getUserRemark())) {
				account.setAccountRemark(user.getUserRemark() + user.getUserName());
			}
			account.setAccountModifyTime(date);
			if (user.getUserState() != null) {
				if (user.getUserState() == 1) {
					account.setAccountState(0);
				} else {
					account.setAccountState(2);
				}
			}
			securityRpcService.updateAccount(account);
		}
		user.setUserUpdateTime(date);
		return this.update(user);
	}


}
